package com.xbongbong.paas.service;

import com.xbongbong.paas.domain.entity.ManualSignEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisConstant;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.manual.dto.ReuseSignatureAddDTO;
import com.xbongbong.pro.manual.dto.ReuseSignatureUpdateDTO;
import com.xbongbong.pro.manual.vo.ReuseSignatureGetVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ManualSignConstant;
import com.xbongbong.saas.model.ManualSignModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @ClassName ManualSignService
 * @Description 手写签名 service层
 * @Date 2022/8/11 10:24
 * @Version 1.0
 **/
@Service
public class ManualSignService {

    public static final Logger LOG = LoggerFactory.getLogger(ManualSignService.class);

    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private ManualSignModel manualSignModel;

    /**
     * 获取复用手签信息
     *
     * @param baseDTO
     * @return
     */
    public ReuseSignatureGetVO getReuseSignature(BaseDTO baseDTO) throws XbbException {
        ReuseSignatureGetVO reuseSignatureGetVO = new ReuseSignatureGetVO();
        String corpid = baseDTO.getCorpid();
        String userId = baseDTO.getUserId();
        Map<Object, Object> manualSignInfo = paasRedisHelper.getAllHashValue(RedisPrefixConstant.MANUAL_SIGN, corpid + userId);
        if (manualSignInfo.size() == 0) {
            //去数据库拿
            Map<Object, Object> manualSignMap = new HashMap<>(BasicConstant.DEFAULT_ACCURACY);
            ManualSignEntity manualSignEntity = manualSignModel.getByUserId(corpid, userId);
            if (Objects.nonNull(manualSignEntity)) {
                //置入缓存
                manualSignMap.put(ManualSignConstant.OPEN_REUSE_SIGNATURE, String.valueOf(manualSignEntity.getEnable()));
                manualSignMap.put(ManualSignConstant.REUSE_SIGNATURE, manualSignEntity.getSign());
                manualSignMap.put(ManualSignConstant.REUSE_SIGNATURE_ID, String.valueOf(manualSignEntity.getId()));
                paasRedisHelper.setAllHashValue(RedisPrefixConstant.MANUAL_SIGN, corpid + userId, manualSignMap, RedisConstant.ONE_HOUR);

                reuseSignatureGetVO.setOpenReuseSignature(manualSignEntity.getEnable());
                reuseSignatureGetVO.setReuseSignature(manualSignEntity.getSign());
                reuseSignatureGetVO.setReuseSignatureId(manualSignEntity.getId());
                return reuseSignatureGetVO;
            } else {
                manualSignMap.put(ManualSignConstant.OPEN_REUSE_SIGNATURE, BasicConstant.ZERO_STRING);
                manualSignMap.put(ManualSignConstant.REUSE_SIGNATURE, "");
                manualSignMap.put(ManualSignConstant.REUSE_SIGNATURE_ID, BasicConstant.ZERO_STRING);
                paasRedisHelper.setAllHashValue(RedisPrefixConstant.MANUAL_SIGN, corpid + userId, manualSignMap, RedisConstant.ONE_HOUR);
                return reuseSignatureGetVO;
            }
        }
        reuseSignatureGetVO.setOpenReuseSignature(Integer.valueOf(String.valueOf(manualSignInfo.get(ManualSignConstant.OPEN_REUSE_SIGNATURE))));
        reuseSignatureGetVO.setReuseSignature(String.valueOf(manualSignInfo.get(ManualSignConstant.REUSE_SIGNATURE)));
        reuseSignatureGetVO.setReuseSignatureId(Long.valueOf(String.valueOf(manualSignInfo.get(ManualSignConstant.REUSE_SIGNATURE_ID))));
        return reuseSignatureGetVO;
    }

    /**
     * 更新复用手签信息
     *
     * @param reuseSignatureUpdateDTO
     * @return
     * @throws XbbException
     */
    public BaseVO updateReuseSignature(ReuseSignatureUpdateDTO reuseSignatureUpdateDTO) throws XbbException {
        String corpid = reuseSignatureUpdateDTO.getCorpid();
        String userId = reuseSignatureUpdateDTO.getUserId();
        Map<Object, Object> manualSignMap = new HashMap<>(BasicConstant.DEFAULT_ACCURACY);
        ManualSignEntity manualSignEntity = new ManualSignEntity();
        manualSignEntity.setId(reuseSignatureUpdateDTO.getReuseSignatureId());
        manualSignEntity.setCorpid(reuseSignatureUpdateDTO.getCorpid());
        manualSignEntity.setUserId(reuseSignatureUpdateDTO.getUserId());
        manualSignEntity.setEnable(reuseSignatureUpdateDTO.getOpenReuseSignature());
        manualSignEntity.setSign(reuseSignatureUpdateDTO.getReuseSignature());
        manualSignEntity.setUpdateTime(DateTimeUtil.getInt());
        manualSignModel.update(manualSignEntity);
        //置入缓存
        manualSignMap.put(ManualSignConstant.OPEN_REUSE_SIGNATURE, String.valueOf(manualSignEntity.getEnable()));
        manualSignMap.put(ManualSignConstant.REUSE_SIGNATURE, manualSignEntity.getSign());
        manualSignMap.put(ManualSignConstant.REUSE_SIGNATURE_ID, String.valueOf(manualSignEntity.getId()));
        paasRedisHelper.setAllHashValue(RedisPrefixConstant.MANUAL_SIGN, corpid + userId, manualSignMap, RedisConstant.ONE_HOUR);
        return new BaseVO();
    }

    /**
     * 新增复用手签信息
     *
     * @param reuseSignatureAddDTO
     * @return
     * @throws XbbException
     */
    public BaseVO addReuseSignature(ReuseSignatureAddDTO reuseSignatureAddDTO) throws XbbException {
        String corpid = reuseSignatureAddDTO.getCorpid();
        String userId = reuseSignatureAddDTO.getUserId();
        Map<Object, Object> manualSignMap = new HashMap<>(BasicConstant.DEFAULT_ACCURACY);
        ManualSignEntity manualSignEntity = manualSignModel.getByUserId(corpid, userId);
        if (Objects.isNull(manualSignEntity)) {
            manualSignEntity = new ManualSignEntity();
            manualSignEntity.setAddTime(DateTimeUtil.getInt());
        }
        manualSignEntity.setCorpid(reuseSignatureAddDTO.getCorpid());
        manualSignEntity.setUserId(reuseSignatureAddDTO.getUserId());
        manualSignEntity.setEnable(reuseSignatureAddDTO.getOpenReuseSignature());
        manualSignEntity.setSign(reuseSignatureAddDTO.getReuseSignature());
        manualSignEntity.setUpdateTime(DateTimeUtil.getInt());
        manualSignEntity.setDel(BasicConstant.ZERO);
        if (Objects.isNull(manualSignEntity.getId())) {
            manualSignModel.insert(manualSignEntity);
        } else {
            manualSignModel.update(manualSignEntity);
        }
        //置入缓存
        manualSignMap.put(ManualSignConstant.OPEN_REUSE_SIGNATURE, String.valueOf(manualSignEntity.getEnable()));
        manualSignMap.put(ManualSignConstant.REUSE_SIGNATURE, manualSignEntity.getSign());
        manualSignMap.put(ManualSignConstant.REUSE_SIGNATURE_ID, String.valueOf(manualSignEntity.getId()));
        paasRedisHelper.setAllHashValue(RedisPrefixConstant.MANUAL_SIGN, corpid + userId, manualSignMap, RedisConstant.ONE_HOUR);
        return new BaseVO();
    }

    /**
     * 删除复用手签信息
     */
    public BaseVO removeReuseSignature(BaseDTO baseDTO) throws XbbException {
        String corpid = baseDTO.getCorpid();
        String userId = baseDTO.getUserId();
        paasRedisHelper.removeValue(RedisPrefixConstant.MANUAL_SIGN, corpid + userId);
        ManualSignEntity manualSignEntity = manualSignModel.getByUserId(corpid, userId);
        if (Objects.nonNull(manualSignEntity)) {
            manualSignEntity.setDel(DelEnum.DELETE.getDel());
            manualSignModel.update(manualSignEntity);
        }
        return new BaseVO();
    }
}
